#include "common.h"

volatile struct adc_reg *adc1 = (volatile struct adc_reg *)0x40012400;

void init_adc_pin(void)
{
    gpioa->CRL &= ~(0x0fffffff);

    gpioc->CRL &= ~(0xff);
}

void init_adc1(void)
{

    adc1 = (volatile struct adc_reg *)0x40012400;
    rcc->APB2ENR |= (1 << 9);
    rcc->APB2RSTR = (1 << 9);
    delay_time(0xffff);
    rcc->APB2RSTR = 0;

    init_adc_pin();

    adc1->CR2 = 0x1a0100;  // 定时器4的CCR4事件触发
                           // adc1->CR2 = 0x1E0100;

    adc1->CR1 |= (1 << 8);

    adc1->SMPR1 = 0x00ffffff;
    adc1->SMPR2 = 0x3fffffff;
    adc1->SQR1  = 0x800000;  // 8
    adc1->SQR2  = (11 << 10) | (10 << 5) | (6 << 0);
    adc1->SQR3  = (5 << 25) | (4 << 20) | (3 << 15) | (2 << 10) | (1 << 5) | (0 << 0);

    adc1->CR2 |= (1 << 3) | (1 << 0);
    while (adc1->CR2 & (1 << 3))
        ;

    adc1->CR2 |= (1 << 2);
    while (adc1->CR2 & (1 << 2))
        ;
}
